package com.avidys.gwt.wom.model;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;

/**
 * a site to monitor
 * @author jm
 * Site as an overlay JS as an overlay type
 */

public class Site extends JavaScriptObject {

	// Overlay types always have protected, zero-arg ctors
	protected Site() { } 

	// Typically, methods on overlay types are JSNI
	public final native String getName() /*-{ return this.name; }-*/;
	public final native String getIP() /*-{ return this.ip; }-*/;
	public final native String getUrl()  /*-{ return this.url;  }-*/;
	public final native int getPort()  /*-{ return this.port;  }-*/;
	public final native int getTimeUp()  /*-{ return this.timeup;  }-*/;
	public final native String getLastCheckTime()  /*-{ return this.lastchecktime;  }-*/;
	public final native String getStartTime()  /*-{ return this.lastchecktime;  }-*/;

	// List<SiteTestPoint> history;
	// List<Integer> ports;

	// Note, though, that methods aren't required to be JSNI
	public final String getStr() {
		return getName() + " " + getUrl() + " " + getPort()+ " " + getTimeUp(); 
	}


	public static final native JsArray<Site> getSites (String json) /*-{
	  return eval(json);
	}-*/;
	
	public final void listSites (JsArray<Site> cs){
		for (int i = 0, n = cs.length(); i < n; ++i) {
			GWT.log(cs.get(i).getStr(),null);
		}
	}
	

	public static final String[] requiredProperties = {"IP", "ports", "test"};


	public static Site getSite(String result) {
		JsArray<Site> sites = Site.getSites(result);
		return sites.get(0);
	}


//	private String IP;
//	private double timeUp;
//	private List<SiteTestPoint> history;
//	/** The ports to monitor */
//	private List<Integer> ports;
	
//	public Site(String ip, List<Integer> ports) {
//		super();
//		IP = ip;
//		this.ports = ports;
//	}
//	public Site(String ip){
//		super();
//		IP = ip;
//	}
	
//	public String getTime(double value){
//		NumberFormat fmt = NumberFormat.getDecimalFormat();
//		// double value = 12345.6789;
//		String formatted = fmt.format(value);
//		// Prints 1,2345.6789 in the default locale
//		GWT.log("Formatted string is" + formatted, null);
//		return formatted;
//	}
//
//	// The class can also be used to convert a numeric string back into a double:
//	public double setTime(String d){
//		double value = NumberFormat.getDecimalFormat().parse(d); // i.e. "12345.6789"
//		GWT.log("Parsed value is" + value, null);
//		return value;
//	}
//	
//	/** last generated error from JSON analysis */
//	public static int lastError =0;
			
/**
 * Take a JSON object and transform it in this
 * Returns null if something goes wrong
 * Populates lastError
 * @param object
 * @return
 */
//	public static Site getOneRecord (JSONObject object){
//		JSONValue jsonValue;
//		JSONObject jsSite;
//		JSONString jsIP;
//		JSONNumber jsTimeElapsed;
//
//		if ((jsSite = object) == null) return null;      
//
//		for (String s : requiredProperties) {
//			if ((jsonValue = jsSite.get(s)) == null) return null;
//		}
//
//		if ((jsonValue = jsSite.get("IP")) == null) return null;
//		if ((jsIP = jsonValue.isString()) == null) return null;
//
//		if ((jsonValue = jsSite.get("timeElapsed")) == null) return null;
//		if ((jsTimeElapsed = jsonValue.isNumber()) == null) return null;
//		
//		Site site = new Site();//	jsIP.stringValue()
////								jsTimeElapsed.doubleValue()
////							);
//		return site;
//	}
//

	
}
